package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDeptLeader;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.vo.SysDeptLeaderSaveVo;
import com.ruoyi.common.core.vo.SysDeptLeaderVo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDeptLeaderService;
import com.ruoyi.system.util.CommonCacheUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 部门领导设置
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/deptLeader")
public class SysDeptLeaderController extends BaseController
{
    @Autowired
    private ISysDeptLeaderService sysDeptLeaderService;

    @PreAuthorize("@ss.hasPermi('system:deptLeader:list')")
    @GetMapping("/list")
    public AjaxResult list(SysDeptLeader sysDeptLeader){

        QueryWrapper<SysDeptLeader> queryWrapper = new QueryWrapper();
        queryWrapper.eq("DEPT_ID", sysDeptLeader.getDeptId());
        List<SysDeptLeader> depts = sysDeptLeaderService.list(queryWrapper);
        List<SysDeptLeaderVo> sysDeptLeaderVoList = new ArrayList<>();
        if(depts!=null && depts.size()>0){
            for (SysDeptLeader ss:depts){
                SysDeptLeaderVo vo = new SysDeptLeaderVo();
                BeanUtils.copyProperties(ss,vo);
                SysUser sysUser = CommonCacheUtil.getUserByIdByCache(ss.getUserId()+"");
                if(sysUser!=null){
                    vo.setUserName(sysUser.getNickName());
                }
                sysDeptLeaderVoList.add(vo);
            }
        }
        return AjaxResult.success(sysDeptLeaderVoList);
    }


    @PreAuthorize("@ss.hasPermi('system:deptLeader:delete')")
    @GetMapping("/delete")
    public AjaxResult delete(String id){

        sysDeptLeaderService.deleteInfo(id);
        return AjaxResult.success();

    }

    @PreAuthorize("@ss.hasPermi('system:deptLeader:add')")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SysDeptLeaderSaveVo saveVo){

        String deptId = saveVo.getDeptId();
        if(StringUtils.isEmpty(deptId)){
            return AjaxResult.error("传递参数出错！");
        }

        QueryWrapper<SysDeptLeader> queryWrapper = new QueryWrapper();
        queryWrapper.eq("DEPT_ID", deptId);
        sysDeptLeaderService.remove(queryWrapper);

        List<SysDeptLeader> tableData = saveVo.getTableData();
        if(tableData!=null && tableData.size()>0){
            for (int i =0 ;i<tableData.size();i++){
                SysDeptLeader deptLeader =  tableData.get(i);
                deptLeader.setDeptId(new Long(deptId));
                sysDeptLeaderService.saveOrUpdateInfo(deptLeader);
            }
        }

        return AjaxResult.success();
    }


}
